﻿using Client.Model;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.CommonViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: MenuTreeViewModel
    /// CreateTime: 2020/5/30 16:40:48
    /// </summary>
    public class MenuTreeViewModel : BindableBase
    {


		private List<MainMenu> menus;

		public List<MainMenu> Menus
		{
			get { return menus; }
			set
			{
				menus = value;
				RaisePropertyChanged("Menus");
			}
		}


        public MenuTreeViewModel(List<MainMenu> menus)
		{
			Menus = menus;
			//Menus = new List<MainMenu>()
			//{
			//	new MainMenu()
			//	{
			//		Name = "主菜单1",
			//		Icon = "Apple",
			//		SubMenus = new List<SubMenu>()
			//		{
			//			new SubMenu()
			//			{
			//				Name = "子菜单1-1",
			//			},
			//			new SubMenu()
			//			{
			//				Name = "子菜单1-2"
			//			}
			//		}
			//	},
			//	new MainMenu()
			//	{
			//		Name = "主菜单2",
			//		Icon = "Bank",
			//		SubMenus = new List<SubMenu>()
			//		{
			//			new SubMenu()
			//			{
			//				Name = "子菜单2-1"
			//			},
			//			new SubMenu()
			//			{
			//				Name = "子菜单2-2"
			//			}
			//		}
			//	}
			//};


			BindCommand();


		}


		private void BindCommand()
		{
			CheckMainMenuCommand = new DelegateCommand<MainMenu>(CheckMainMenu);
			CheckSubMenuCommand = new DelegateCommand<SubMenu>(CheckSubMenu);

			CheckItemCommand = new DelegateCommand<ViewItem>(CheckItem);
		}


		public DelegateCommand<MainMenu> CheckMainMenuCommand { get; set; }
		private void CheckMainMenu(MainMenu menu)
		{
			bool? isChecked = menu.Checked;
			menu.SubMenus.ForEach(sm =>
			{
				sm.Checked = isChecked;
			});
		}

		public DelegateCommand<SubMenu> CheckSubMenuCommand { get; set; }
		private void CheckSubMenu(SubMenu menu)
		{
			MainMenu mainMenu = menu.Parent;
			if (menu.Checked == false)
			{
				if (mainMenu.SubMenus.All(sub => sub.Checked == false))
				{
					mainMenu.Checked = false;
				} else
				{
					mainMenu.Checked = null;
				}
			}
			if (menu.Checked == true)
			{
				if (mainMenu.SubMenus.All(sub=>sub.Checked == true))
				{
					mainMenu.Checked = true;
				} else
				{
					mainMenu.Checked = null;
				}
			}
		}






		private List<ViewItem> items = new List<ViewItem>()
		{
			new ViewItem()
            {
				Item = "aaa"
            },
			new ViewItem()
			{
				Item = "bbb"
			},
			new ViewItem()
			{
				Item = "ccc"
			},
			new ViewItem()
			{
				Item = "ddd"
			},
			new ViewItem()
			{
				Item = "eee"
			},
		};

		public List<ViewItem> Items
		{
			get { return items; }
			set
			{
				items = value;
				RaisePropertyChanged("Items");
			}
		}

		public List<ViewItem> CheckedItems { get; set; } = new List<ViewItem>();
        private string checkedItemsStr = "hello world";

        public string CheckedItemsStr
        {
            get { return checkedItemsStr; }
            set
            {
                checkedItemsStr = value;
                RaisePropertyChanged("CheckedItemsStr");
            }
        }

        public DelegateCommand<ViewItem> CheckItemCommand { get; set; }
		private void CheckItem(ViewItem item)
        {
			if (item.Checked)
            {
				if (!CheckedItems.Contains(item))
                {
					CheckedItems.Add(item);
				}
            } else
            {
				if (CheckedItems.Contains(item))
                {
					CheckedItems.Remove(item);
                }
            }

			List<string> strList = CheckedItems.Select(it => it.Item).ToList();
			CheckedItemsStr = string.Join(",", strList);

        }






	}
}
